<?php
namespace Adminx\Controller;

class ChexingController extends AdminController {
	#列表
	public function index() {
        $brandID = I('brandID');  
        $carName = I('carName');  

        if ($brandID!='') {
            $map['brandID'] = $brandID;
        }

        if ($carName!='') {
            $map['carName'] = array('like','%'.$carName.'%');
        }

		$obj = M('Chexing');
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 15);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $list[$key]['num'] = M('Car')->where(array('chexingID'=>$value['id']))->count();
        }
        $this->assign('list', $list);
        $this->assign('page', $show);

        unset($map);
        $map['fid']=0;
        $map['model']=2;
        $cateList = M('Category')->where($map)->order('sort asc,id asc')->select();
        $this->assign('cateList',$cateList);

        $daili = M('User')->select();
        $this->assign('daili',$daili);
        $this->display();
	}

	//添加
	public function add(){
        if($_POST){    
            $this->all_add('Chexing');
        }else{ 
            $this->display();
        }
    }

	//编辑
	public function edit(){
		if($_POST){    
            $this->all_save('Chexing',U('Chexing/index'));
        }else{          
            $id = I('get.id');
            if ($id=='' || !is_numeric($id)) {
                $this->error('参数错误');
            }
            $list = M('Chexing')->where(array('id'=>$id))->find();

            if ($list) {
                $this->assign('list',$list);
                $this->display();
            }else{
                $this->error('信息不存在');
            }
        }
	}

    #删除
	public function del() {
        $id=I('post.selectedids');
        if($id==''){
            $this->error("您没有选择任何信息！");
        }else{
            foreach ($id as $v) {
                M('Car')->where(array('chexingID'=>$v))->delete();
                M('Chexing')->where(array('id'=>$v))->delete();                
            }
            $this->success("删除成功",U('Chexing/index'));
        }    
	}


    public function car(){
        $chexingID = I('get.chexingID');
        if (!isset ($chexingID) || !is_numeric($chexingID)) {
            $this->error('参数错误');
        }

        //相册信息
        $map['id']=$chexingID;
        $chexing=M('Chexing')->where($map)->find();
        if(!$chexing){
            $this->error("车型不存在");
        }
        unset($map);
        $map['chexingID']=$chexingID;
        $count = M('Car')->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 15);
        $show = $page->show();
        $list = M('Car')->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('chexing', $chexing);
        $this->display();
    }

    //添加图片
    public function addcar(){
        if($_POST){
            $this->all_add('Car',U('Chexing/car',array('chexingID'=>I("chexingID"))));
        }else{
            $chexingID = I('get.chexingID');
            if (!isset ($chexingID) || !is_numeric($chexingID)) {
                $this->error('参数错误');
            }
            //相册信息
            $map['id']=$chexingID;
            $chexing=M('Chexing')->where($map)->find();
            if(!$chexing){
                $this->error("车型不存在");
            }
            $this->assign('chexing', $chexing);
            $this->display();
        }
    }

    //编辑图片
    public function editcar(){
        if($_POST){
            $this->all_save('Car',U('Chexing/car',array('chexingID'=>I("chexingID"))));
        }else{
            $id = I('get.id');
            if (!isset ($id) || !is_numeric($id)) {
                $this->error('参数错误');
            }
            $map["id"]=$id;
            $list=M('Car')->where($map)->find();
            if($list){
                //相册信息
                unset($map);
                $map['id']=$list['chexingID'];
                $chexing=M('Chexing')->where($map)->find();
                if(!$chexing){
                    $this->error("车型不存在");
                }
                $this->assign('chexing', $chexing);
                $this->assign('list', $list);
                $this->display();
            }else{
                $this->error("信息不存在");
            }
        }
    }
    //删除图片
    public function delcar(){
        $this->all_del('Car','reload');
    }
}
?>